package com.llkj.qingnianguoxue.circle;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jm.chat.pickerimage.utils.StringUtil;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.album.AlbumActivity;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.CircleDetailBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.login.AgreementActivity;
import com.llkj.qingnianguoxue.util.DialogUtil;
import com.llkj.qingnianguoxue.util.FileOperation;
import com.llkj.qingnianguoxue.util.FileProvider7;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;
import com.llkj.qingnianguoxue.util.PicCameraLocalUtil;
import com.llkj.qingnianguoxue.util.RxBus;
import com.llkj.qingnianguoxue.util.SelectPictureUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.im.android.api.model.UserInfo;
import rx.functions.Action1;

public class CreateCircleActivity extends BaseActivity implements DialogUtil.DialogClickListener,SelectPictureUtil.SelectPictureResultCallback{

    private Dialog dialogPhotoPicture;
    private File cameraFile;//
    private boolean isPermissions;//相机权限
    private String mCoverPath;

    @BindView(R.id.ivCircleCover)
    protected ImageView mIvCircleCover;//封面
    @BindView(R.id.etCircleName)
    protected EditText etCircleName;//圈子名
    @BindView(R.id.etCircleDescription)
    protected EditText etCircleDescription;//圈子简介
    @BindView(R.id.etCircleMaxCount)
    protected EditText etCircleMaxCount;//圈子人数上限
    @BindView(R.id.etCirclePrice)
    protected EditText etCirclePrice;//入圈费用
    @BindView(R.id.etCircleDuration)
    protected EditText etCircleDuration;//入圈时限
    @BindView(R.id.ll_agree)
    protected LinearLayout llAgree;//同意协议
    @BindView(R.id.tv_agreement)
    protected TextView tvAgreemnet;//协议

    private SelectPictureUtil mSelectPictureUtil;//选择图片

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_circle);
        setTitle("创建圈子", true, TITLE_TYPE_IMG, R.drawable.icon_back, false,-1, -1);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        ButterKnife.bind(this);
        new RxPermissions(this)
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        isPermissions = aBoolean;
                    }
                });

        llAgree.setSelected(true);
        mSelectPictureUtil = new SelectPictureUtil(this,this);
    }

    @OnClick({R.id.rlUploadCorver, R.id.ll_agree, R.id.tv_agreement, R.id.btnCreateCircle})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.rlUploadCorver://上传封面
                if (dialogPhotoPicture == null) {
                    dialogPhotoPicture = DialogUtil.showPhotoPictureDialog(this,this);
                }
                dialogPhotoPicture.show();
                break;
            case R.id.ll_agree://同意协议
                llAgree.setSelected(!llAgree.isSelected());
                break;
            case R.id.tv_agreement://协议
                Bundle bundle = new Bundle();
                bundle.putInt("from_class", 3);
                startActivity(AgreementActivity.class, bundle);
                break;
            case R.id.btnCreateCircle://创建圈子
                if (StringUtil.isEmpty(mCoverPath)) {
                    ToastUtil.makeLongText(this,getString(R.string.check_circle_cover));
                    return;
                }
                String circleName = etCircleName.getText().toString();
                if(StringUtils.isEmpty(circleName)){
                    ToastUtil.makeLongText(this,getString(R.string.check_circle_name));
                    return;
                }
                String circleDes = etCircleDescription.getText().toString();
                if(StringUtils.isEmpty(circleDes)){
                    ToastUtil.makeLongText(this,getString(R.string.check_circle_description));
                    return;
                }
                String circleMax = etCircleMaxCount.getText().toString();
                if(StringUtils.isEmpty(circleMax) || Integer.parseInt(circleMax)<=0 || Integer.parseInt(circleMax)>=500){
                    ToastUtil.makeLongText(this,getString(R.string.check_circle_max));
                    return;
                }
                String circlePrice= etCirclePrice.getText().toString();
                if(StringUtils.isEmpty(circlePrice)  || Float.parseFloat(circlePrice)<0){
                    ToastUtil.makeLongText(this,getString(R.string.check_circle_price));
                    return;
                }
                String circleDuration = etCircleDuration.getText().toString();
                if(StringUtils.isEmpty(circleDuration) || Integer.parseInt(circleDuration)<=0){
                    ToastUtil.makeLongText(this,getString(R.string.check_circle_duration));
                    return;
                }
                if(!llAgree.isSelected()){
                    ToastUtil.makeLongText(this,getString(R.string.check_agree));
                    return;
                }

                loadWaitDialog();
                HttpMethod.addCircle(this,this,circleName,circleDes,circleMax,circlePrice,circleDuration, MyApplication.getInstance().sputil.getString(ConstantUserInfo.CHAT_LOGIN_ID),new File(mCoverPath));
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mSelectPictureUtil.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onCropResult(File file) {
        cameraFile = file;
        mCoverPath = cameraFile.getAbsolutePath();
        ImageLoadingUtil.loadingImg(mIvCircleCover, mCoverPath, 0);
    }

    @Override
    public void dialogClick(int type) {
        switch (type) {
            case 1://拍照
                if (isPermissions) {
                    mSelectPictureUtil.takePhoto();
                } else {
                    ToastUtil.makeLongText(this, getResources().getString(R.string.permission_camera));
                }
                break;
            case 2://相册
                mSelectPictureUtil.album();
                break;
        }
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_ADD_CIRCLE) {
            CircleDetailBean detailBean = GsonUtil.GsonToBean(data,CircleDetailBean.class);
            if(detailBean!=null){

            }
            ToastUtil.makeLongText(this,getString(R.string.create_circle_success));
            setResult(RESULT_OK);
            RxBus.getInstance().post(ConstantUserInfo.REFRESH_CIRCLE);

            myFinish();
        }
    }

    @Override
    protected void onhttpFailed(String message, int method) {
        ToastUtil.makeLongText(this,"对不起，圈子创建失败");
    }
}
